﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using NHibernate;
using hbse.Entities;


namespace hbse
{
    public partial class Uscustomer : UserControl
    {
        private TabItem newTab;
        private DevComponents.DotNetBar.TabControl tabControl2;
        private ISession session;
        private Users users;
        private Customer customer;

        public Uscustomer(DevComponents.DotNetBar.TabControl _tabControl2, TabItem _newTab, ISession _session,Users _users)
        {
            InitializeComponent();
            newTab = _newTab;
            tabControl2 = _tabControl2;
            session = _session;
            users = _users;
            loadDefalt();
        }
        public void loadDefalt()
        {
            using (session.BeginTransaction())
            {
                var customers = session.CreateQuery("from Customer ").Future<Customer>();
                if (customers.Count() > 0)
                {
                    dgvcus.DataSource = customers.ToList();
                    Formatdgvcus();
                }
            }
        }
        private void Formatdgvcus()
        {
            if (dgvcus.RowCount > 0)
            {
                dgvcus.Columns["CustomerCode"].HeaderText = "CustomerCode";
                dgvcus.Columns["CustomerName"].HeaderText = "CustomerName";


                dgvcus.Columns["CustomerCode"].Width = 100;
                dgvcus.Columns["CustomerName"].Width = 100;
                dgvcus.Columns["CustomerPrints"].Visible = false;
                dgvcus.Columns["AddDate"].Visible = false;
                dgvcus.Columns["Status"].Visible = false;
                dgvcus.Columns["Users"].Visible = false;
                dgvcus.Columns["Id"].Visible = false;
             
            }
        }

        private void cmdsearch_Click(object sender, EventArgs e)
        {
            using (session.BeginTransaction())
            {
                var customers = session.CreateQuery("from Customer c where c.CustomerCode like '%"+txtSearch.Text.Trim()+"%'").Future<Customer>();
                dgvcus.DataSource = customers.ToList();
                Formatdgvcus();
            }

        }

        private void cmdadd_Click(object sender, EventArgs e)
        {
            if (txtcuscode.Text.Trim() == "")
            {
                DevComponents.DotNetBar.MessageBoxEx.Show("กรุณาป้อนรหัสก่อน !!!", "ผลการตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                txtcuscode.Focus();
                return;
            }
            if (txtcusName.Text.Trim() == "")
            {
                DevComponents.DotNetBar.MessageBoxEx.Show("กรุณาป้อนชื่อก่อน !!!", "ผลการตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                txtcusName.Focus();
                return;
            }

            var customer = session.CreateQuery("from Customer c where c.CustomerCode='" + txtcuscode.Text.Trim() + "'").Future<Customer>();
            if (customer.Count() > 0) {

                DevComponents.DotNetBar.MessageBoxEx.Show("กรุณาป้อนรหัสซ้ำ !!!", "ผลการตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                txtcuscode.Focus();
                return;
            }

            Customer newCustomer = new Customer();
            newCustomer.CustomerCode = txtcuscode.Text.Trim();
            newCustomer.CustomerName = txtcusName.Text.Trim();
            newCustomer.Users = users;
            newCustomer.AddDate = DateTime.Now;
            newCustomer.Status = "1";

            using (ITransaction tx = session.BeginTransaction())
            {
                session.Save(newCustomer);
                session.Flush();
                tx.Commit();
            }
            loadDefalt();

        }

        private void dgvcus_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.RowIndex == -1)
            {
                return;
            }

            txtcuscode.Text = dgvcus.Rows[e.RowIndex].Cells["CustomerCode"].Value.ToString();
            txtcusName.Text = dgvcus.Rows[e.RowIndex].Cells["CustomerName"].Value.ToString();
            
        }

        private void cmdedit_Click(object sender, EventArgs e)
        {
            if (txtcuscode.Text.Trim() == "")
            {
                DevComponents.DotNetBar.MessageBoxEx.Show("กรุณาป้อนรหัสก่อน !!!", "ผลการตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                txtcuscode.Focus();
                return;
            }
            if (txtcusName.Text.Trim() == "")
            {
                DevComponents.DotNetBar.MessageBoxEx.Show("กรุณาป้อนชื่อก่อน !!!", "ผลการตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                txtcusName.Focus();
                return;
            }
            var customer = session.CreateQuery("from Customer c where c.CustomerCode='" + txtcuscode.Text.Trim() + "'").Future<Customer>();
           
            if (customer.Count() <= 0)
            {

                DevComponents.DotNetBar.MessageBoxEx.Show("ไม่พบข้อมูล !!!", "ผลการตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                txtcuscode.Focus();
                return;
            }

            
            Customer editCustomer = (Customer)customer.First();

            editCustomer.CustomerCode = txtcuscode.Text.Trim();
            editCustomer.CustomerName = txtcusName.Text.Trim();
            editCustomer.Users = users;
            editCustomer.AddDate = DateTime.Now;
            editCustomer.Status = "1";

            using (ITransaction tx = session.BeginTransaction())
            {
                session.SaveOrUpdate(editCustomer);
                session.Flush();
                tx.Commit();
            }
            loadDefalt();


        }

        private void cmddelete_Click(object sender, EventArgs e)
        {
            var customer = session.CreateQuery("from Customer c where c.CustomerCode='" + txtcuscode.Text.Trim() + "'").Future<Customer>();

            if (customer.Count() <= 0)
            {

                DevComponents.DotNetBar.MessageBoxEx.Show("ไม่พบข้อมูล !!!", "ผลการตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                txtcuscode.Focus();
                return;
            }

            Customer delCustomer = (Customer)customer.First();

            using (ITransaction tx = session.BeginTransaction())
            {
                session.Delete(delCustomer);
                session.Flush();
                tx.Commit();
            }
            loadDefalt();


        }

    }
}
